<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined( '_JEXEC' ) or die( 'Restricted access' );

class iJoomla_SurveysControllerEditsurvey extends iJoomla_SurveysController{
	
	function __construct() {	  
		parent::__construct();
			
		$this->registerTask('', 'editsurvey');
		$this->registerTask('save_survey', 'saveSurvey');		
		$this->registerTask('back', 'back');
		$this->registerTask('viewresult', 'viewResult');
		$this->registerTask('edit_details', 'editDetails');
	}			
	
	function editsurvey(){		
		JRequest::setVar('view', 'Editsurvey');	
		parent::display();
	}
	
	function editDetails(){
		$model = $this->getModel('editsurvey');
		$model->editDetails();
	}
	
	function viewResult(){
		$survey_id = JRequest::getVar("id", "0");
		$model = $this->getModel('editsurvey');
		$show_result = $model->showResult($survey_id);
		if($show_result == "0"){// NO
			$this->setRedirect(JURI::root());
		}
		else{// YES
			$model->editResult();
		}	
	}
	
	function saveSurvey(){
		$model = $this->getModel('editsurvey');						
				
		$survey_id = JRequest::getVar("id", "", "post");
		$current_page_id = JRequest::getVar("current_page_id", "", "post");
		$page_details = $model->getPageDetails($current_page_id);
		$page_order = $page_details["0"]["ordering"];
		$pages = $model->getPrevNextPage($survey_id, $current_page_id, $page_order);
		$survey_id_alias = JRequest::getVar("survey_id_alias", "", "post");		
		$prev_page = $pages["prev"];
		$next_page = $pages["next"];
		JRequest::setVar("next_page_id", $next_page);
		JRequest::setVar("previews_page_id", $prev_page);		
		$action = JRoute::_('index.php?option=com_surveys&controller=editsurvey&Itemid='.JRequest::getVar("Itemid").'&id='.$survey_id_alias.'&page='.$next_page);

		$model->save();
		
		$_SESSION["prev_page"] = $current_page_id;		
		$_SESSION["terminate"] = JRequest::getVar("previews_page_id", "");
		unset($_SESSION["view_result"]);
		$this->setRedirect($action);
	}
	
	function back(){		
		$model = $this->getModel('editsurvey');						
				
		$survey_id = JRequest::getVar("id", "", "post");
		$current_page_id = JRequest::getVar("current_page_id", "", "post");	
		$page_details = $model->getPageDetails($current_page_id);
		$page_order = $page_details["0"]["ordering"];
		$pages = $model->getPrevNextPage($survey_id, $current_page_id, $page_order);		
		$survey_id_alias = JRequest::getVar("survey_id_alias", "", "post");
		$prev_page = $pages["prev"];
		$next_page = $pages["next"];
		if(isset($_SESSION["prev_page_array"])){
			$array = $_SESSION["prev_page_array"];
			unset($array[end($array)]);
			$prev_page = end($array);			
			$_SESSION["prev_page_array"] = $array;
		}
		$action = JRoute::_('index.php?option=com_surveys&controller=editsurvey&Itemid='.JRequest::getVar("Itemid").'&id='.$survey_id_alias.'&page='.$prev_page);
		$_SESSION["view_result"] = "true";
		$this->setRedirect($action);
	}
}

?>